package com.smasher.print.em;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.smasher.print.core.PrintDelegate;
import com.printsdk.PrintSerializable;

import org.json.JSONObject;

public class Printer5820 implements PrintDelegate, Handler.Callback {

    public static final String TAG = "Print5820";

    public static PrintSerializable mPrinter = new PrintSerializable();

    private final Connect5820 mConnect5820;

    private final PrintContent printContent;


    public Printer5820(Context context) {
        String SDK_Version = mPrinter.getSDKVersion();
        Log.d(TAG, "initParams SDK_Version: " + SDK_Version);
        mConnect5820 = new Connect5820USB(context, mPrinter);

        Handler mHandler = new Handler(Looper.getMainLooper(), this);
        printContent = new PrintContent(mPrinter, mHandler);

        mConnect5820.start(context);
    }


    // 连接打印机
    @Override
    public void connPrint(Context context) {
        mConnect5820.connPrint(context);
    }

    // 连接/断开连接打印机
    @Override
    public void disConnPrint(Context context) {
        mConnect5820.disConnPrint(context);
    }

    @Override
    public boolean isUnConnect(Context context) {
        return mConnect5820.isUnConnect(context);
    }


    @Override
    public void printText(JSONObject json) {
        if (printContent != null) {
            printContent.printText(json);
        }
    }

    @Override
    public void release(Context context) {
        mConnect5820.stop(context);
    }

    @Override
    public boolean handleMessage(@NonNull Message msg) {
        if (msg.what == 50000) {
            printEnd();
            return true;
        }
        return false;
    }


    private void printEnd() {
        Log.d(TAG, "printEnd: ");
    }
}
